import React, {Component} from "react";
import { Select } from 'antd';
import { connect } from "dva";

@connect(({driver, loading}) => ({
  driverList: driver.driverList
}))
export class DriverSelect extends React.Component {

  handleSearch = value => {
    const { dispatch } = this.props
    if (value) {
      dispatch({
        type: 'driver/selectList',
        payload: { driver: value }
      })
    } else {
      this.setState({ data: [] });
    }
  };

  onSelect = value => {
    const { dispatch, driverList } = this.props
    let payload = {}
    driverList.forEach(driver => {
      if(driver.driver === value){
        payload = driver
      }
    })
    if(!payload.driver){
      payload.driver = value
    }
    dispatch({
      type: 'splitOrder/handleSelectDriver',
      payload: payload
    })
  }
  render() {
    const options = this.props.driverList.map(d => <Option key={d.driver}>{d.driver}</Option>);
    return (
      <Select
        showSearch
        value = {this.props.value}
        placeholder="请选择司机"
        defaultActiveFirstOption={false}
        onSelect={this.onSelect}
        showArrow={false}
        filterOption={false}
        onSearch={this.handleSearch}
        notFoundContent={null}
      >
        {options}
      </Select>
    );
  }
}
